;(function($, window, document, undefined) {
    var _doc = $(document),
        $body = $('body');
    methods = {
        init: function(options) {
            return this.each(function() {
                var $this = $(this),
                    opt = $this.data('hDialog');
                if (typeof(opt) == 'undefined') {
                    var defaults = {
                        title: '', //弹框标题
                        box: '#HBox', //弹框默认选择器
                        boxBg: '#fff', //弹框默认背景颜色
                        modalBg: 'rgba(0,0,0,0.78)', //遮罩默认背景颜色
                        closeBg: '#ccc', //弹框关闭按钮默认背景颜色
                        width: 300, //弹框默认宽度
                        height: 270, //弹框默认高度
                        positions: 'center', //弹框位置(默认center：居中，top：顶部居中，left：顶部居左)
                        triggerEvent: 'click', //触发方式(默认click：点击，mouseenter：悬浮)
                        effect: 'hide', //弹框关闭效果(默认hide，淡出关闭：fadeOut)
                        resetForm: true, //是否清空表单(默认true：清空，false：不清空)
                        modalHide: true, //是否点击遮罩背景关闭弹框(默认true：关闭，false：不可关闭)
                        closeHide: true, //是否隐藏关闭按钮(默认true：不隐藏，false：隐藏)
                        escHide: true, //是否支持ESC关闭弹框(默认true：关闭，false：不可关闭)
                        beforeShow: function() {}, //显示前的回调方法
                        afterHide: function() {} //隐藏后的回调方法
                    };
                    opt = $.extend({}, defaults, options);
                    $this.data('hDialog', opt);
                }
                opt = $.extend({}, opt, options);
                $(opt.box).hide(); //隐藏容器
                $this.on(opt.triggerEvent, function() { //元素点击事件
                    //重置表单
                    if (opt.resetForm) {
                        var $obj = $(opt.box);
                        $obj.find('input[type=text],textarea').val('');
                        $obj.find('select option').removeAttr('selected');
                        $obj.find('input[type=radio],input[type=checkbox]').removeAttr('checked');
                    }
                    //支持ESC关闭
                    if (opt.escHide) {
                        $(document).keyup(function(event) {
                            switch (event.keyCode) {
                                case 27:
                                    methods.close(opt);
                                    break;
                            }
                        });
                    }
                    methods.fire.call(this, opt.beforeShow); //调用显示之前回调函数
                    methods.add(opt, $this); //显示弹框
                    //点击关闭事件
                    var $close = $('#HCloseBtn');
                    if (opt.modalHide) {
                        $close = $('#HOverlay,#HCloseBtn');
                    }
                    $close.on('click', function(event) {
                        event = event || window.event;
                        event.stopPropagation();
                        methods.close(opt);
                    });
                });
            });
        },
        add: function(o, $this) { //显示弹框
            var w, h, t, l, m;
            $obj = $(o.box);
            title = o.title;
            c = $this.attr("class");
            modalBg = o.modalBg;
            closeBg = o.closeBg;
            w = o.width != undefined ? parseInt(o.width) : '300';
            h = o.height != undefined ? parseInt(o.height) : '270';
            m = "" + (-(h / 2)) + 'px 0 0 ' + (-(w / 2)) + "px";
            //弹框位置
            switch (o.positions) {
                case 'center':
                    t = l = '50%';
                    break;
                case 'top':
                    t = 0;
                    l = '50%';
                    m = "0 0 0 " + (-(w / 2)) + "px";
                    break;
                case 'left':
                    t = l = m = 0;
                    break;
                default:
                    t = l = '50%';
            }
            methods.remove('#HOverlay,#HCloseBtn,#HTitle');
            $body.stop().append("<div id='HOverlay' style='width:" + _doc.width() + "px;height:" + _doc.height() + "px;background-color:" + modalBg + ";position:fixed;top:0;left:0;z-index:9999;'></div>");
            if (o.title != '') {
                $obj.stop().prepend('<div id="HTitle" style="padding:10px 45px 10px 12px;border-bottom:1px solid #ddd;background-color:#f2f2f2;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">' + o.title + '</div>');
            }
            if (o.closeHide != false) {
                $obj.stop().append('<a id="HCloseBtn" title="关闭" ></a>').css({
                    'position': 'fixed',
                    'backgroundColor': o.boxBg,
                    'top': t,
                    'left': l,
                    'margin': m,
                    'zIndex': '100000'
                });
            }
            $obj.stop().animate({
                'width': o.width,
                'height': o.height
            }, 300).removeAttr('class').addClass('animated ' + c + '').show();
        },
        close: function(o, urls) { //关闭弹框
            var $obj = $(o.box);
            //关闭效果
            switch (o.effect) {
                case "hide":
                    $obj.stop().hide(_effect);
                    break;
                case "fadeOut":
                    $obj.stop().fadeOut(_effect);
                    break;
                default:
                    $obj.stop().hide(_effect);
            }
            function _effect() {
                methods.remove('#HOverlay,.HTooltip');
                $(this).removeAttr('class').removeAttr('style').addClass('animated').hide();
                if (urls != undefined) {
                    setTimeout(function() {
                        window.location.href = urls;
                    }, 1000);
                }
                methods.fire.call(this, o.afterHide); //隐藏后的回调方法
            }
        },
        remove: function(a) { //移除元素
            $(a).remove();
        },
        fire: function(event, data) { //调用回调函数
            if ($.isFunction(event)) {
                return event.call(this, data);
            }
        }
    };
    $.fn.hDialog = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Error! Method' + method + 'does not exist on jQuery.hDialog！');
        }
    };
    /**
    +----------------------------------------------------------
    * 内置扩展
    +----------------------------------------------------------
    */
    $.extend({
        //滚动的时候，右边二维码在小于40的时候固定在顶部
        fixed: function() {
            $(window).scroll(function(event) {
                if ($(window).scrollTop() > 80) {
                    $("#fixedWIndow").css({
                        'position': 'fixed',
                        'top': 'auto'
                    }).animate({
                        bottom: 90
                    }, 1000);
                } else {
                    $("#fixedWIndow").css({
                        'position': 'absolute',
                        'top': '0'
                    });
                }
                return false;
            });
        },
        goTop: function(b, r) {
            b = b != undefined ? b : '30px';
            r = r != undefined ? r : '20px';
            methods.remove('#HGoTop');
            $("#fixedWIndow").stop().append('<a id="HGoTop" href="javascript:;" class="animated"><i></i> 返回顶部</a>').find('#HGoTop').hide();
            $(window).scroll(function() {
                if ($(window).scrollTop() > 100) {
                    $('#HGoTop').removeClass('fadeInUp fadeOutDown').addClass('fadeInUp').show();
                } else {
                    $('#HGoTop').removeClass('fadeInUp fadeOutDown').addClass('fadeOutDown');
                }
            });
            //返回顶部按钮点击事件
            $('#HGoTop').on('click', function() {
                $('body,html').animate({
                    scrollTop: 0
                }, 500);
                return false;
            });
        },
        fourBtn: function() {
            $(".DwnloadNow").on('click', function() {
                $(".downloadBox").removeClass('hide').siblings(".giftBox").addClass('hide');
            });
            $(".giftNow").on('click', function() {
                $(".giftBox").removeClass('hide').siblings(".downloadBox").addClass('hide');
            });
        }
    });
    $.fn.extend({
        //tab
        tab: function() {
            var i = 0;
            $('#tabIndex>a').click(function() {
                i = $(this).index();
                $(this).addClass('active').siblings().removeClass('active');
                $('#tabCont>ul').fadeOut();
                $('#tabCont>ul').eq(i).stop(true).fadeIn();
            });
        },
        fixedWindow: function() {
            $(window).scroll(function() {
                if ($(window).scrollTop() > 100) {
                    $('#HGoTop').removeClass('fadeInUp fadeOutDown').addClass('fadeInUp').show();
                } else {
                    $('#HGoTop').removeClass('fadeInUp fadeOutDown').addClass('fadeOutDown');
                }
            });
        },
	 //友情链接
        friendLink: function(opt) {
            if (!opt) var opt = {};
            var _btnUp = $("#" + opt.up);
            var _btnDown = $("#" + opt.down);
            var timerID;
            var _this = this.eq(0).find("ul:first");
            var lineH = _this.find("li:first").height() + 20,
                line = opt.line ? parseInt(opt.line, 10) : parseInt(this.height() / lineH, 10),
                speed = opt.speed ? parseInt(opt.speed, 10) : 500;
            timer = opt.timer
            if (line == 0) line = 1;
            var upHeight = 0 - line * lineH / 3;
            var scrollUp = function() {
                _btnUp.unbind("click", scrollUp);
                _this.animate({
                    marginTop: upHeight
                }, speed, function() {
                    for (i = 1; i <= line; i++) {
                        _this.find("li:first").appendTo(_this);
                    }
                    _this.css({
                        marginTop: 0
                    });
                    _btnUp.bind("click", scrollUp);
                });
            }
            var scrollDown = function() {
                _btnDown.unbind("click", scrollDown);
                for (i = 1; i <= line; i++) {
                    _this.find("li:last").show().prependTo(_this);
                }
                _this.css({
                    marginTop: upHeight
                });
                _this.animate({
                    marginTop: 0
                }, speed, function() {
                    _btnDown.bind("click", scrollDown);
                });
            }
            var autoPlay = function() {
                if (timer) timerID = window.setInterval(scrollUp, timer);
            };
            var autoStop = function() {
                if (timer) window.clearInterval(timerID);
            };
            _this.hover(autoStop, autoPlay).mouseout();
            _btnUp.css("cursor", "pointer").click(scrollUp).hover(autoStop, autoPlay);
            _btnDown.css("cursor", "pointer").click(scrollDown).hover(autoStop, autoPlay);
        },
	//精灵展示
        fairy: function() {
            $("#fairyList").delegate('li', 'click', function(event) {
                $("#fairyList").find("li").removeClass('active')
                $(this).addClass('active');
            });
        }
    });
    //进度条
    var Progress = function(elem, options) {
        this.$element = elem,
            this.defaults = {
                'background': '#19dcff',
                'width': '50%'
            },
            this.opt = $.extend({}, this.defaults, options);
    }
    Progress.prototype = {
        progressBar: function() {
            return this.$element.css({
                'background': this.opt.background,
                'width': this.opt.width
            });
            return this.$element.animate({
                    'background': this.opt.background,
                    'width': this.opt.width
                },
                50);
        }
    };
    $.fn.progres = function(options) {
            var progress = new Progress(this, options);
            return progress.progressBar();
        }
       
})(jQuery, window, document);
$(function() {
    //引导页的弹出层
    $('.DwnloadNow').hDialog({
        width: 625,
        height: 422,
        boxBg: 'transparent'
    });
    $('.giftNow').hDialog({
        width: 625,
        height: 422,
        boxBg: 'transparent'
    });
    $.fourBtn();
    $("#lookMore").on('mouseover', function() {
        $(this).find(".recomList").show();
    }).on('mouseout', function() {
        $(this).find(".recomList").hide();
    });
    $.goTop();
    $.fixed();
});
